package com.gitee.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.order.dao.OrderDao;
import com.gitee.order.entity.OrderEntity;
import com.gitee.order.feign.StockFeignService;
import com.gitee.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    private StockFeignService stockFeignService;

    @Override
    @Transactional
    @GlobalTransactional
    public void createOrder(String sno, Long productId, Integer count) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(sno);
        orderEntity.setMemberId(1L);

        baseMapper.insert(orderEntity);

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        stockFeignService.reduct(productId, count);

        //调用第三个服务
        if ((count & 1) == 0) {     //偶数
            int a = 1 % 0;
        }
    }
}
